package com.quick.develop.flowable.framework.convert.model;

import com.quick.develop.flowable.constant.BpmConstants;
import com.quick.develop.flowable.domain.dto.model.BpmModelElementsDTO;
import com.quick.develop.flowable.utils.ExtensionElementUitls;
import org.flowable.bpmn.model.CallActivity;
import org.flowable.bpmn.model.ExtensionElement;
import org.flowable.bpmn.model.UserTask;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author junyuan.liu
 * @description:
 * @date 2022/8/26 15:07
 */
@Mapper
public interface BpmModelElementsConvert {
    BpmModelElementsConvert INSTANCE = Mappers.getMapper(BpmModelElementsConvert.class);

    default List<BpmModelElementsDTO> convertList(List<UserTask> userTasks, List<CallActivity> activities) {

        List<BpmModelElementsDTO> list = Optional.ofNullable(userTasks).map(this::convertList).orElse(Collections.emptyList());
        List<BpmModelElementsDTO> list2 = Optional.ofNullable(activities).map(this::convertList1).orElse(Collections.emptyList());
        List<BpmModelElementsDTO> listAll = Stream.concat(list.stream(),list2.stream()).collect(Collectors.toList());
        Collections.sort(listAll);//编译通过；
//        for (BpmModelElementsDTO person : listAll) {
//            System.out.println(person);
//        }
        //针对排序做处理
        return listAll;
    }

    @Mappings({
            @Mapping(target = "multiInstance", expression = "java(bean.hasMultiInstanceLoopCharacteristics())"),
            @Mapping(target = "type",  constant = BpmConstants.USER_TASK),
            @Mapping(source = "extensionElements", target = "exeSort", qualifiedByName = "convertSort")
    })
    BpmModelElementsDTO convert(UserTask bean);

    List<BpmModelElementsDTO> convertList(List<UserTask> list);

    @Mappings({
            @Mapping(target = "multiInstance", expression = "java(bean.hasMultiInstanceLoopCharacteristics())"),
            @Mapping(target = "type",  constant = BpmConstants.CALL_ACTIVITY),
            @Mapping(source = "extensionElements", target = "exeSort", qualifiedByName = "convertSort")
    })
    BpmModelElementsDTO convert(CallActivity bean);
    List<BpmModelElementsDTO> convertList1(List<CallActivity> list);

    @Named("convertSort")
    default int calSort(Map<String, List<ExtensionElement>> extensionElements){
        int df = 0;
//        List<ExtensionElement> listChild = ExtensionElementUitls.getChildExtensionList(extensionElements);

        String value = ExtensionElementUitls.getEleMentStr(extensionElements,"sort");
        if(value!=null){
            try {
                df = Integer.valueOf(value);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return df;
    }

}
